﻿using dz.loger.core;
using dz.util.core;
using Microsoft.AspNetCore.Razor.TagHelpers;

namespace dz.u9.webapi
{
    public class U9CTokenUtil
    {
        private System.Timers.Timer __timer;
        private string __token;

        private static readonly Lazy<U9CTokenUtil> instance = new Lazy<U9CTokenUtil>(() => new U9CTokenUtil());

        public static U9CTokenUtil Instance
        {
            get
            {
                return instance.Value;
            }
        }


        private U9CTokenUtil()
        {
            InitTimer();
        }
        private void InitTimer()
        {
            //设置定时间隔(毫秒为单位)
            int interval = 1000 * 60 * 180;
            __timer = new System.Timers.Timer(interval);
            //设置执行一次（false）还是一直执行(true)
            __timer.AutoReset = true;
            //设置是否执行System.Timers.Timer.Elapsed事件
            __timer.Enabled = true;
            //绑定Elapsed事件
            __timer.Elapsed += new System.Timers.ElapsedEventHandler(TimerUp);

            __BuildToken();
        }

        public string GetToken()
        {
            return __token;
        }
        public string GetToken(bool isRefresh = true)
        {
            //if (isRefresh)
            //{
            //    __BuildToken();

            //}
            loger.core.LogUtil.Instance.Debug("开始获得token");
            __BuildToken();
            LogUtil.Instance.Debug("结束获得token");
            return __token;
        }
        private void __BuildToken()
        {
            Dictionary<string, string> args = new Dictionary<string, string>();

            LogUtil.Instance.Debug(Config.Instance.GetValue("ClientId"));
            LogUtil.Instance.Debug(Config.Instance.GetValue("ClientSecret"));
            LogUtil.Instance.Debug(Config.Instance.GetValue("EntCode"));
            LogUtil.Instance.Debug(Config.Instance.GetValue("UserCode"));
            LogUtil.Instance.Debug(Config.Instance.GetValue("OrgCode"));


            args.Add("clientid", Config.Instance.GetValue("ClientId"));
            args.Add("clientsecret", Config.Instance.GetValue("ClientSecret"));
            args.Add("entCode", Config.Instance.GetValue("EntCode"));
            args.Add("userCode", Config.Instance.GetValue("UserCode"));
            args.Add("orgCode", Config.Instance.GetValue("OrgCode"));

            FormUrlEncodedContent content = new FormUrlEncodedContent(args);
            string url = Config.Instance.GetValue("u9url") + "OAuth2/AuthLogin";

            HttpUtil util = new HttpUtil();

            try
            {
                WebResult result = util.Get(url, content).Result;


                __token = JsonUtil.GetData<string>(result.Data, "Data");
            }
            catch (Exception ex)
            {
                LogUtil.Instance.Error("获得token失败");

                throw new Exception("获得token失败");
            }
            

            
        }
        private void TimerUp(object? sender, System.Timers.ElapsedEventArgs e)
        {
            __BuildToken();
        }
    }
}
